<template>
    <div>
        <el-table :data="userlist" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" />
            <el-table-column prop="username" label="用户名称" />
            <el-table-column prop="rolename" label="所属角色" />
            <el-table-column label="状态">
                <template #default="item">
                    <div>
                        <el-tag type="success" v-if="item.row.status == 1">启用</el-tag>
                        <el-tag type="danger" v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination class="page" small background layout="prev, pager, next" :page-size="getSize" :total="total"
            @current-change="changePage" />
    </div>
</template>

<script lang="ts" setup>
import { userDel } from '@/request/api';
import { defineEmits, computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex';
const store = useStore()
const userlist = computed(() => store.getters['user/getUserlist'])
const total = computed(() => store.getters['user/getCount'])
const getSize = computed(() => store.getters['user/getSize'])
const getUser = () => {
    store.dispatch('user/UserListActions')
}
const getCount = () => {
    store.dispatch('user/changeCountAction')
}
const getPage = (n: number) => {
    store.dispatch('user/changePageAction', n)
}
getUser()
getCount()

let changePage = (n: number) => {
    console.log(n, "当前页");
    getPage(n);
}
let emit = defineEmits(['edit'])
let edit = (id: number) => {
    emit('edit', id)
}

let del = (uid: number) => {
    ElMessageBox.confirm(
        '你确定要删除这条数据吗?',
        '提示警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            userDel({
                uid
            }).then((res: any) => {
                console.log(res, '管理员删除');
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: res.msg
                    })
                    getUser()
                    getCount()
                    return
                }
                ElMessage({
                    type: 'info',
                    message: res.msg
                })
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
</script>

<style scope>
.page {
    position: absolute;
    right: 20px;
    margin-top: 10px;
}
</style>
